package my.mall.admin.api.config

import my.mall.admin.api.pojo.login.LoginInfo
import my.mall.common.annotation.Login
import my.mall.common.enums.RespMetaEnum
import my.mall.common.exception.WebBaseException
import org.springframework.core.MethodParameter
import org.springframework.lang.Nullable
import org.springframework.stereotype.Component
import org.springframework.web.bind.support.WebDataBinderFactory
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer

@Component
class LoginResolver : HandlerMethodArgumentResolver {
    override fun supportsParameter(parameter: MethodParameter): Boolean {
        return parameter.hasParameterAnnotation(Login::class.java)
    }

    override fun resolveArgument(
        parameter: MethodParameter,
        @Nullable container: ModelAndViewContainer?,
        request: NativeWebRequest,
        @Nullable factory: WebDataBinderFactory?
    ): Any {
        val adminId = 1L
            ?: throw WebBaseException(RespMetaEnum.NOT_LOGIN) //request.getAttribute("adminId", SCOPE_REQUEST);
        if (parameter.parameterType.isAssignableFrom(Long::class.java)) {
            return adminId
        }
        val storeId = 1L
            ?: throw WebBaseException(RespMetaEnum.NO_STORE) //request.getAttribute("storeId", SCOPE_REQUEST);
        return LoginInfo(storeId, adminId)
    }
}
